//
//  WYBrandController.m
//  Boss
//
//  Created by admin on 2018/6/25.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYBrandController.h"
#import "WYBusinessRecommendCell.h"
#import "WYBusinessOpportunity.h"
#import "WYBusinessDetailController.h"
@interface WYBrandController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation WYBrandController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    if (self.typeID.length) {
        [self loadDataWithID:@""];
    }else{
       [self loadData];
    }
    
    
}
-(void)loadDataWithID:(NSString *)ID
{
    WEAKSELF
    [WYHttpServer getMoreBrandZoneByType:self.typeID WithCurrentPage:@"1" WithPageSize:@"10" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            
            [weakSelf.dataArray addObject:[WYBusinessOpportunity getBusinessOpportunityModel:response[@"result"][@"detail"]]];
            
            [weakSelf.tableView reloadData];
            
        }
        
        
        
        
    }];
}

-(void)loadData
{
    WEAKSELF
    [WYHttpServer getMoreBrandZoneWithCurrentPage:@"1" WithPageSize:@"10" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            [weakSelf.dataArray addObject:[WYBusinessOpportunity getBusinessOpportunityModel:response[@"result"][@"detail"]]];
            
            [weakSelf.tableView reloadData];
            
        }
        
        
        
        
        
    }];
}
-(void)setupUI
{
    if (!self.navTitle.length) {
         self.navigationItem.title = @"品牌专区";
    }else{
        self.navigationItem.title = self.navTitle;
    }
   
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYBusinessRecommendCell" bundle:nil] forCellReuseIdentifier:@"WYBusinessRecommendCell"];
    self.tableView.estimatedRowHeight = 200;
    self.tableView.tableFooterView = [[UIView alloc]init];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYBusinessRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYBusinessRecommendCell" forIndexPath:indexPath];
    WYBusinessOpportunity *bus = self.dataArray[indexPath.section][indexPath.row];
    [cell setBusiness:bus];
    
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYBusinessOpportunity *bus = self.dataArray[indexPath.section][indexPath.row];
    WYBusinessDetailController *bdc = [[WYBusinessDetailController alloc]init];
    bdc.urlString = bus.businessId;
    bdc.navTitle = bus.businessName;
    [self.navigationController pushViewController:bdc animated:YES];
    
    
    
}




- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}
@end
